import React, { Component } from 'react'
import { Redirect, withRouter } from 'dva/router';
import {interceptor} from "dcloud-core";

/**
 * 权限路由
 *
 */

class PrivateRoute extends Component {
    // 拦截器方法
     getInterceptor(location) {
        let arrHref= false;
        let arrIncludes= false;
        interceptor.hrefName.forEach((item)=>{
            if(location.pathname == item){
                arrHref = true
                return false
            }
        })

        interceptor.includes.forEach((item)=>{
            if(location.pathname.includes(item)){
                arrIncludes = true
                return false
            }
        })
        return arrHref || arrIncludes
    }

    render() {
        const {location} = this.props
        const isLogged = sessionStorage.getItem('token') ? true : false
        if(isLogged || this.getInterceptor(location)){
            return (
                <div>
                    { this.props.children }
                </div>
            )
        }else {
            return (
                <Redirect to="/login" />
            )
        }

    }
}

// export default ListData;
export default withRouter(PrivateRoute);
